package com.zy.web.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zy.common.core.domain.model.LoginUser;
import com.zy.common.enums.DeviceType;
import com.zy.common.enums.LoginType;
import com.zy.common.exception.app.PasswordErrorException;
import com.zy.common.exception.user.UserException;
import com.zy.common.exception.user.UserPasswordNotMatchException;
import com.zy.common.helper.LoginHelper;
import com.zy.web.domain.ZyUser;
import com.zy.web.mapper.ZyUserMapper;
import com.zy.web.service.IZySystemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import static cn.dev33.satoken.stp.StpUtil.checkLogin;

/**
 * 系统业务层
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ZySystemServiceImpl implements IZySystemService {


    private final ZyUserMapper mapper;

    @Override
    public String login(String username, String password, String code, String uuid) {
        ZyUser user = loadUserByUserNameOrPhone(username);
//        String hashpw = BCrypt.hashpw();
        if (!BCrypt.checkpw(password, user.getPassword())) {
            throw new UserPasswordNotMatchException();
        }
        // 此处可根据登录用户的数据不同 自行创建 loginUser
        LoginUser loginUser = buildLoginUser(user);
        // 生成token
        LoginHelper.loginByDevice(loginUser, DeviceType.PC);
        return StpUtil.getTokenValue();
    }

    /**
     * 构建登录用户
     */
    private LoginUser buildLoginUser(ZyUser user) {
        LoginUser loginUser = new LoginUser();
        loginUser.setId(user.getId());
        loginUser.setUsername(user.getUsername());
        loginUser.setXh(user.getXh());
        loginUser.setLxfs(user.getLxfs());
        loginUser.setSf(user.getSf());
        return loginUser;
    }


    private ZyUser loadUserByUserNameOrPhone(String nameOrPhone) {
        ZyUser user = mapper.selectUserByUserNameOrPhone(nameOrPhone);
        if (ObjectUtil.isNull(user)) {
            log.info("登录用户：{} 不存在.", nameOrPhone);
            throw new UserException("user.not.exists", nameOrPhone);
        } else if (user.getDeleted()) {
            log.info("登录用户：{} 已被删除.", nameOrPhone);
            throw new UserException("user.password.delete", nameOrPhone);
        }
        return user;
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code     验证码
     * @param uuid     唯一标识
     */
    public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
//        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
//        String captcha = RedisUtils.getCacheObject(verifyKey);
//        RedisUtils.deleteObject(verifyKey);
//        if (captcha == null) {
//            asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
//            throw new CaptchaExpireException();
//        }
//        if (!code.equalsIgnoreCase(captcha)) {
//            asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
//            throw new CaptchaException();
//        }
    }
}
